﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Twi.COMMON.Core;
using System.Xml.Linq;

namespace Twi.DataAccess.OutInterface
{
    /// <summary>
    /// 新增、编辑、删除、查询等基本表操作接口
    /// </summary>
    public interface ICRUD
    {
        /// <summary>
        /// 简单新增操作
        /// </summary>
        /// <param name="data">json对象</param>
        /// json新增格式：
        /// {
        ///     TableName:"TDuty",
        ///     IDField:'FID',
        ///     Fields:{
        ///         FName:"开发人员",
        ///         FSort:"8",
        ///         FID:"SEQ_User.nextval",
        ///         FDate:"to_date('2013-11-12','yyyy-mm-dd')"
        ///     },
        ///     FnFields:'FID,FDate',
        ///     MaxCodeFields:'FID,FCode'
        /// }
        /// <remarks>
        ///    FnFields 为采用数据库函数取值字段配置集合,
        ///    MaxCodeFields 为从最大记录编号表中取值的字段集合
        /// </remarks>
        /// <returns>新增记录的ID</returns>
        string SimpleNew(string data);

        /// <summary>
        /// 简单新增操作
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="dictFields">字段及其值字典</param>
        /// <param name="fnFields">FnFields 为采用数据库函数取值字段配置集合</param>
        /// <param name="dictMaxCodeFields">为从最大记录编号表中取值的字段集合</param>
        /// <returns>新增记录的ID</returns>
        string SimpleNew(string tableName, Dictionary<string, object> dictFields, Dictionary<string, string> dictFnFields = null, Dictionary<string, string> dictMaxCodeFields = null);

        /// <summary>
        /// 简单编辑操作
        /// </summary>
        /// <param name="data">json对象</param>
        /// <example>
        /// json编辑格式：
        /// {
        ///     TableName:"TDuty",
        ///     Fields:{
        ///         FName:"开发人员",
        ///         FSort:"8"
        ///     },
        ///     Condition:{
        ///         FID:""
        ///     }
        /// }
        /// </example>
        void SimpleEdit(string data);

        /// <summary>
        /// 简单编辑操作
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="dictUpdateFields">要修改的字段及其值字典</param>
        /// <param name="dictConditionFields">简单条件字典（所谓简单，就是条件都是 = 和 and 连接）</param>
        void SimpleEdit(string tableName, Dictionary<string, object> dictUpdateFields, Dictionary<string, object> dictConditionFields);

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="id">ID的值(如果有多个，用逗号隔开)</param>
        /// <param name="idField">ID字段（default:FID）</param>
        void Delete(string tableName, string id,string idField="FID");

        /// <summary>
        /// 多项删除(事务)
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="ids">FIDs</param>
        /// <param name="idField">ID字段（default:FID）</param>
        void Delete(string tableName, string[] ids, string idField = "FID");

                /// <summary>
        /// 根据删除状态删除数据（事务）
        /// </summary>
        /// <param name="data">JSON</param>
        /// <example>
        /// json编辑格式：
        /// {
        ///     TableName:"TDuty",
        ///     IDs:'ID1,ID2',
        ///     IDField:'FID', //默认FID
        ///     DeleteField : "FIsDeleted", //默认FIsDeleted
        ///     Value:"1" //默认 1
        /// }
        /// </example>
        void DeleteBySetFlag(string data);

                /// <summary>
        /// 根据删除状态删除数据（事务）
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="ids">IDs</param>
        /// <param name="idField">ID字段名</param>
        /// <param name="deleteField">删除状态字段名</param>
        /// <param name="value">删除状态设置的值</param>
        void DeleteBySetFlag(string tableName, string[] ids, string idField = "FID", string deleteField = "FIsDeleted", string value = "1");

        /// <summary>
        /// 新增或编辑
        /// </summary>
        /// <param name="data">约定的json格式(新增时，IDField不传，则默认为FID)</param>
        /// <remarks>
        /// 由于参数是直接从web从传递的所以为了保证安全性，对该方法进行了如下限制：
        /// 1、该方法仅支持简单的新增和编辑操作，Conditon表示编辑的条件，逻辑关系为“=”，多条件为“and”连接
        /// 2、对应复杂的组合条件，大家可以自己写方法。
        /// 3、SortModel为启用智能排序功能，如果没有该项则不需要
        /// </remarks>
        /// <example>
        /// json新增格式：
        /// {
        ///     TableName:"TDuty",
        ///     NewOrEdit:"New", 
        ///     IDField:'FID',
        ///     Fields:{
        ///         FName:"开发人员",
        ///         FSort:"8"
        ///     },
        ///     SortModel: {   
        ///        SortField:'FSort',
        ///        SortValue:'',
        ///        FilterField:'TOptionType',
        ///        FilterValue:''
        ///     }    
        /// }
        /// json编辑格式：
        /// {
        ///     TableName:"TDuty",
        ///     NewOrEdit:"Edit",
        ///     Fields:{
        ///         FName:"开发人员",
        ///         FSort:"8"
        ///     },
        ///     Condition:{
        ///         FID:""
        ///     },
        ///     SortModel: {   
        ///        SortField:'FSort',
        ///        SortValue:'',
        ///        FilterField:'TOptionTypeID',
        ///        FilterValue:''
        ///     },
        ///     FnFields:'FID,FDate',
        ///     MaxCodeFields:'FID,FCode' 
        /// }
        /// 对新增操作，如果没有指定FID，则会自动生成。
        /// </example>
        /// <remarks>
        ///    FnFields 为采用数据库函数取值字段配置集合,
        ///    MaxCodeFields 为从最大记录编号表中取值的字段集合
        /// </remarks>
        /// <returns>返回FID</returns>
        string NewOrEdit(string data);

        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="selectFields">查询列默认所有*</param>
        /// <returns></returns>
        DataTable GetAll(string tableName, string selectFields = "*", string orderFields = null);

        /// <summary>
        /// 根据ID获取记录
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="id">ID值</param>
        /// <param name="idField">ID字段名（default:FID）</param>
        /// <param name="selectFields">查询字段（default:*）</param>
        /// <param name="needMappingTable">是否需要表映射</param>
        /// <returns></returns>
        DataTable GetByID(string tableName, string id, string idField = "FID", string selectFields = "*", bool needMappingTable = true);

        /// <summary>
        /// 根据ID获取第一行第一列的值
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="id">ID值</param>
        /// <param name="idField">ID字段名（default:FID）</param>
        /// <param name="selectFields">查询字段（default:*）</param>
        /// <param name="needMappingTable">是否需要表映射</param>
        /// <returns></returns>
        object GetScalarByID(string tableName, string id, string idField = "FID", string selectFields = "*", bool needMappingTable = true);
        /// <summary>
        /// 简单条件查询
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="data">json对象</param>
        /// <example>
        /// json格式：
        /// {
        ///     TableName:"TDuty",
        ///     SelectFields:"FID,FName,FSort", (如果不传，则默认查询所有*)
        ///     OrderFields:'',//不传则表示不排序
        ///     Condition:{
        ///         FID:""
        ///     },
        ///     SimpleCondition:{
        ///         Field:'FName',
        ///         Value:'志',
        ///         COperator:'Like'
        ///     },
        ///     TreeCondition:{
        ///         Field:'FDepartmentTCode',
        ///         Value:'101'
        ///     }
        /// }
        /// </example>
        DataTable GetBySimpleCondition(string data);



        /// <summary>
        /// 简单条件分页查询
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="data">json对象</param>
        /// <param name="paging">PagingCondition对象</param>
        /// <example>
        /// json格式：
        /// {
        ///     TableName:"TDuty",
        ///     SelectFields:"FID,FName,FSort", (如果不传，则默认查询所有*)
        ///     OrderFields:'',//不传则表示不排序
        ///     Condition:{
        ///         FID:""
        ///     },
        ///     SimpleCondition:{
        ///         Field:'FName',
        ///         Value:'志',
        ///         COperator:'Like'
        ///     },
        ///     TreeCondition:{
        ///         Field:'FDepartmentTCode',
        ///         Value:'101'
        ///     }
        /// }
        /// </example>
        DataTable GetPagingDataByCondition(string data, PagingCondition paging = null);


        /// <summary>
        /// 条件查询【分页】
        /// </summary>
        /// <param name="start">跳过系列中指定的条数</param>
        /// <param name="limit">每页显示条数</param>
        /// <param name="twiData">前台发给服务器端的JSON字符串</param>
        /// <remarks>
        /// Condition:对象配置等于条件（=）
        /// Query：对象配置模糊查询条件（like）
        /// </remarks>
        /// <returns></returns>
        DataTable GetPagingDataByCondition(int start, int limit, string twiData);


        /// <summary>
        /// 根据条件查询个数
        /// </summary>
        /// <param name="data">json对象</param>
        /// <example>
        /// json编辑格式：
        /// {
        ///     TableName:"TDuty",
        ///     Condition:{
        ///         FID:""
        ///     }
        /// }
        /// </example>
        /// <returns></returns>
        int GetCountByCondition(string data);

        /// <summary>
        /// 根据SQL-MAP查询数据
        /// </summary>
        /// <param name="data">json对象</param>
        /// <example>
        /// json参数格式：
        /// {
        ///     CommandName:"",
        ///     Parameters:{
        ///         FID:"",
        ///         FName:''
        ///     }
        /// }
        /// </example>
        /// <returns></returns>
        DataTable GetBySQLMap(string data);

        /// <summary>
        /// 根据SQL-MAP查询数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        DataTable GetBySQLMap(SQLMapModel model);

        /// <summary>
        /// 根据SQL-MAP查询分页数据
        /// </summary>
        /// <param name="data"></param>
        /// <param name="paging"></param>
        /// <returns></returns>
        DataTable GetPagingDataBySQLMap(string data, PagingCondition paging = null);

        /// <summary>
        /// 根据SQL-MAP执行SQL
        /// </summary>
        /// <param name="data">json对象</param>
        /// <example>
        /// json参数格式：
        /// {
        ///     CommandName:"",
        ///     Parameters:{
        ///         FID:"",
        ///         FName:''
        ///     }
        /// }
        /// </example>
        void ExecuteBySQLMap(string data);

        /// <summary>
        /// 根据SQL-MAP执行SQL
        /// </summary>
        /// <param name="model"></param>
        void ExecuteBySQLMap(SQLMapModel model);

        /// <summary>
        /// 根据SQL-MAP查询数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        XElement GetXmlBySQLMap(SQLMapModel model);

        XElement GetXmlBySQLMap(string data);

        XElement SimpleInsertXml(XElement data);

        XElement SimpleUpdateXml(XElement data);

        XElement GetSingleXml(XElement data);

        XElement DeleteXml(XElement data);

        /// <summary>
        /// 获取数据库时间
        /// </summary>
        /// <returns></returns>
        string GetDBNowDateTime();
    }
}
